package com.wiscamp.ninechapters.problems.adapter.resources.tasks;

import com.wiscamp.ninechapters.problems.application.dto.commands.tasks.CreateAssignmentCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.tasks.UpdateAssignmentCommand;
import com.wiscamp.ninechapters.problems.application.dto.vo.tasks.AssignmentVO;
import com.wiscamp.ninechapters.problems.application.services.AssignmentAppService;
import cube.ddd.adapter.BaseController;
import cube.ddd.adapter.RestResponse;
import cube.ddd.application.command.DeleteCommand;
import cube.ddd.aspect.HandleResourceException;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * 分发记录服务端点
 *
 * @author yifeng
 */
@RestController
@RequestMapping("/api/Problems")

@Tag(name = "题库-习题集分发记录信息")
public class AssignmentController extends BaseController {

    private final AssignmentAppService assignmentAppService;

    public AssignmentController(AssignmentAppService assignmentAppService) {
        this.assignmentAppService = assignmentAppService;
    }

    /**
     * 通过习题集分发记录编号获取分发记录
     *
     * @param request
     * @param response
     * @param assignmentId
     * @return
     */
    @GetMapping("Assignments/{assignmentId}")
    @Operation(summary = "通过习题集分发记录编号获取分发记录")
    @HandleResourceException
    public RestResponse<AssignmentVO> getAssignment(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                    @PathVariable("assignmentId") long assignmentId) {
        return ResourceUtils.getById(request, response, assignmentId, assignmentAppService::getAssignment);
    }

    /**
     * 根据习题集编号获取分发记录列表
     *
     * @param setId
     * @return 习题集分发记录列表
     */
    @GetMapping("ProblemSets/{setId}/Assignments")
    @Operation(summary = "根据习题集编号获取分发记录列表")
    @HandleResourceException
    public RestResponse<Collection<AssignmentVO>> getAssignmentBySet(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                     @PathVariable("setId") long setId) {
        return ResourceUtils.getListById(request, response, setId, assignmentAppService::getAssignmentsBySet);
    }

    /**
     * 创建习题集分发记录
     *
     * @param command
     * @return
     */
    @PostMapping("Assignments")
    @Operation(summary = "创建习题集分发记录")
    @HandleResourceException
    public RestResponse<AssignmentVO> createAssignment(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                       @RequestBody CreateAssignmentCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, assignmentAppService::createAssignment);
    }

    /**
     * 更新习题集分发记录
     *
     * @param command
     * @return
     */

    @PutMapping("Assignments")
    @Operation(summary = "更新习题集分发记录")
    @HandleResourceException
    public RestResponse<AssignmentVO> updateAssignment(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                       @RequestBody UpdateAssignmentCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, assignmentAppService::updateAssignment);
    }


    /**
     * 根据习题集分发记录编号删除分发记录
     *
     * @param id
     * @return
     */
    @DeleteMapping("Assignments")
    @Operation(summary = "根据习题集分发记录编号删除分发记录")
    @HandleResourceException
    public RestResponse<Boolean> deleteAssignment(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                  @RequestBody DeleteCommand command) {
        return ResourceUtils.delete(request, response, command, assignmentAppService::deleteAssignment);
    }

    @DeleteMapping("Assignments/{assignmentId}")
    @Operation(summary = "根据习题集分发记录编号删除分发记录")
    @HandleResourceException
    public RestResponse<Boolean> deleteAssignmentById(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                 @PathVariable("assignmentId") long assignmentId) {
        var command = new DeleteCommand();
        command.setId(assignmentId);
        return ResourceUtils.delete(request, response, command, assignmentAppService::deleteAssignment);
    }
}
